﻿using System;
using System.Collections;
using ATMConst;
using ATMCommonExceptions;

namespace ATMCell
{
	[Serializable]
    public class AtmCell
    {
		public enum Size
		{
			Hec = 8,
			Vpi = 12,
			Vci = 16,
			Payload = 3,
			Priority = 1
		}
		public AtmCell( byte[] dataToSend, 
                        BitArray hec, 
                        BitArray vpi,
                        BitArray vci,
                        BitArray payload,
                        BitArray priority)
		{
			data = new byte[Const.ATM_DATA_SIZE];
			try
			{
                if (dataToSend.Length > Const.ATM_DATA_SIZE)
                    throw new AtmCellSizeError(dataToSend);// too many bytes to send
                header = new AtmHeader(hec,
                                        vpi,
                                        vci,
                                        payload,
                                        priority);

                for (var i = 0; i < dataToSend.Length; i++)
                    data[i] = dataToSend[i];
				for (var i = dataToSend.Length; i < Const.ATM_DATA_SIZE; i++)
					data[i] = 0;
			}
            catch (AtmCellSizeError e)
            {
                Console.WriteLine(e.ToString());
                throw;
            }
		}
		[Serializable]
		public class AtmHeader
        {
            public AtmHeader(   BitArray hec,
                                BitArray vpi,
                                BitArray vci,
                                BitArray payload,
                                BitArray priority)
            {
                this.hec = new BitArray(hec); // maybe calculate this there?
                this.vpi = new BitArray(vpi);
                this.vci = new BitArray(vci);
                payLoadType = new BitArray(payload);
                cellLossPriority = new BitArray(priority);
            }

			public BitArray hec { get; set; }
			public BitArray vci { get; set; }
        	public BitArray vpi { get; set; }
			public BitArray payLoadType { get; set; }
			public BitArray cellLossPriority { get; set; }
        }
		public AtmHeader header { get; private set; }
		public byte[] data { get; private set; }
    }
}
